﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace DotNetExtensions.Core.Converters
{
    public class BaseValueConverter
    {
        public virtual string ToString(object obj)
        {
            if(obj == null) return null; ;
            return obj.ToString();
        }
        public virtual string Convert(object obj)
        {
            if (obj == null) return null;
            if (obj.GetType() == typeof(int))
            {
                return obj.ToString();
            }
            else if (obj.GetType() == typeof(int?))
            {
                return obj.ToString();
            }
            else if (obj.GetType() == typeof(string))
            {
                return $"\"{obj}\"";
            }
            else if (obj.GetType() == typeof(long))
            {
                return $"\"{obj}\"";
            }
            else if (obj.GetType() == typeof(long?))
            {
                return $"\"{obj}\"";
            }
            else if (obj.GetType() == typeof(decimal))
            {
                return $"\"{obj}\"";
            }
            else if (obj.GetType() == typeof(decimal?))
            {
                return $"\"{obj}\"";
            }
            else if (obj.GetType() == typeof(byte))
            {
                return $"\"{obj}\"";
            }
            else if (obj.GetType() == typeof(DateTime))
            {
                return $"\"{((DateTime)obj).ToString("yyyy/MM/dd HH:mm:ss")}\"";
            }
            else if (obj.GetType() == typeof(DateTime?))
            {
                DateTime? value = ((DateTime?)obj);
                if (value.HasValue)
                {
                    return $"\"{value.Value.ToString("yyyy/MM/dd HH:mm:ss")}\"";
                }
                return "null";
            }
            else if (obj.GetType().IsClass)
            {
                return JsonConvert.SerializeObject(obj);
            }

            return "null";
        }
    }
}
